<?php
/**
 * 商品分类 API 测试
 */

// 模拟 HTTP 请求
function mockRequest($method, $url, $data = []) {
    echo "=== 测试 {$method} {$url} ===\n";
    
    // 模拟数据库数据
    $mockCategories = [
        [
            'id' => 1,
            'name' => '电子产品',
            'fid' => 0,
            'sort' => 1,
            'status' => 1,
            'created_at' => time(),
            'updated_at' => time()
        ],
        [
            'id' => 2,
            'name' => '手机',
            'fid' => 1,
            'sort' => 1,
            'status' => 1,
            'created_at' => time(),
            'updated_at' => time()
        ],
        [
            'id' => 3,
            'name' => '电脑',
            'fid' => 1,
            'sort' => 2,
            'status' => 1,
            'created_at' => time(),
            'updated_at' => time()
        ]
    ];
    
    if ($method === 'GET' && $url === '/admin/goods-cat') {
        // 构建树形结构
        $tree = buildCategoryTree($mockCategories);
        
        $response = [
            'code' => 20000,
            'msg' => '获取成功',
            'data' => [
                'list' => $tree,
                'total' => count($mockCategories)
            ]
        ];
        
        echo "响应: " . json_encode($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";
        return $response;
    }
    
    if ($method === 'POST' && $url === '/admin/goods-cat') {
        $response = [
            'code' => 20000,
            'msg' => '添加成功',
            'data' => [
                'id' => 4,
                'name' => $data['name'],
                'fid' => $data['parent_id'] ?? 0,
                'sort' => $data['sort'] ?? 0,
                'status' => 1,
                'created_at' => time(),
                'updated_at' => time()
            ]
        ];
        
        echo "响应: " . json_encode($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";
        return $response;
    }
    
    if ($method === 'POST' && $url === '/admin/goods-cat/delete') {
        $response = [
            'code' => 20000,
            'msg' => '删除成功',
            'data' => null
        ];
        
        echo "响应: " . json_encode($response, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . "\n";
        return $response;
    }
    
    return null;
}

// 构建树形结构函数
function buildCategoryTree($categories) {
    $tree = [];
    $map = [];
    
    // 创建映射
    foreach ($categories as $category) {
        $map[$category['id']] = $category;
        $map[$category['id']]['children'] = [];
    }
    
    // 构建树形结构
    foreach ($map as $id => $category) {
        if ($category['fid'] == 0) {
            $tree[] = $category;
        } else {
            if (isset($map[$category['fid']])) {
                $map[$category['fid']]['children'][] = $category;
            }
        }
    }
    
    // 按排序字段排序
    sortTree($tree);
    
    return $tree;
}

// 递归排序树形结构
function sortTree(&$tree) {
    // 按sort字段排序
    usort($tree, function($a, $b) {
        return $a['sort'] - $b['sort'];
    });
    
    // 递归排序子节点
    foreach ($tree as &$item) {
        if (!empty($item['children'])) {
            sortTree($item['children']);
        }
    }
}

// 测试获取分类列表
echo "测试获取分类列表:\n";
$listResponse = mockRequest('GET', '/admin/goods-cat');

echo "\n测试添加分类:\n";
$addResponse = mockRequest('POST', '/admin/goods-cat', [
    'name' => '测试分类',
    'parent_id' => 0,
    'sort' => 1
]);

echo "\n测试删除分类:\n";
$deleteResponse = mockRequest('POST', '/admin/goods-cat/delete', [
    'id' => 1
]);

echo "\n=== 测试完成 ===\n"; 